package com.caiyi.financial.nirvana.sms.util;

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ValidateUtil {

    /**
     * 判断空
     *
     * @param value
     * @return
     */
    public static boolean isNotNll(String value) {
        if (StringUtils.isBlank(value)) {
            return false;
        }
        return true;
    }

    /**
     * 判断长度是否大于数据库长度
     *
     * @param value
     * @param length
     * @return
     */
    public static boolean isLength(String value, int length) {
        if (value.length() > length) {
            return true;
        }
        return false;
    }

    /**
     * 判断是否正确手机号码
     *
     * @param phone
     * @return
     */
    public static boolean isPhone(String phone) {
        if (CommUtil.getPhoneOperator(phone) == 0) {
            return false;
        }
        return true;
    }

    /**
     * 判断是否为合法IP
     *
     * @param ipAddress
     * @return
     */
    public static boolean isIp(String ipAddress) {
        String ip = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";
        Pattern pattern = Pattern.compile(ip);
        Matcher matcher = pattern.matcher(ipAddress);
        return matcher.matches();
    }


    /**
     * 校验是否为url
     *
     * @param url
     * @return
     */
    public static boolean isUrl(String url) {
        String check = "^([hH][tT]{2}[pP]:/*|[hH][tT]{2}[pP][sS]:/*|[fF][tT][pP]:/*)(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~\\/])+(\\?{0,1}(([A-Za-z0-9-~]+\\={0,1})([A-Za-z0-9-~]*)\\&{0,1})*)$";
        Pattern pattern = Pattern.compile(check);
        Matcher matcher = pattern.matcher(url);
        return matcher.matches();
    }

    /**
     * 判断是否为整数
     *
     * @param str
     * @return
     */
    public static boolean isNumeric(String str) {
        for (int i = str.length(); --i >= 0; ) {
            int chr = str.charAt(i);
            if (chr < 48 || chr > 57)
                return false;
        }
        return true;
    }
}
